﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;

using ProgramMain.Classifiers;
using ProgramMain.Framework;
using ProgramMain.OpticNet;
using ProgramMain.Parameters;

namespace ProgramMain
{
	public partial class FrmMain : DevExpress.XtraEditors.XtraForm
	{
	    public string ScreenDeviceName;

		public FrmMain()
		{
			InitializeComponent();

            this.Icon = Framework.GlobalData.DefaultIcon();
            
            InitSkins();

            InitTabbedMDI();

		    nbcMain.OptionsNavPane.NavPaneState = Properties.Settings.Default.NavigatorState
		                                              ? DevExpress.XtraNavBar.NavPaneState.Expanded
		                                              : DevExpress.XtraNavBar.NavPaneState.Collapsed;
		    timerUpdateCheck.Interval = 5*60*1000;
		}

		#region Skins

		private void InitSkins()
		{
			barManager1.ForceInitialize();
			
            foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins)
			{
                if (cnt.SkinName != DevExpress.LookAndFeel.UserLookAndFeel.DefaultSkinName)
                {
                    BarButtonItem item = new BarButtonItem(barManager1, cnt.SkinName);
                    mnuPaintStyle.AddItem(item);
                    item.ItemClick += new ItemClickEventHandler(OnSkinClick);
                }
			}

            SetSkinCaption();
        }

		private void OnSkinClick(object sender, ItemClickEventArgs e)
		{
            ProgramMain.Skins.ApplicationSkins.SetSkinSettings(e.Item.Caption);
            SetSkinCaption();
		}

        private void SetSkinCaption()
        {
            string skinName = ProgramMain.Skins.ApplicationSkins.GetSkinSettings();

            barManager1.GetController().PaintStyleName = "Skin";
            mnuPaintStyle.Caption = skinName;
            mnuPaintStyle.Hint = mnuPaintStyle.Caption;
            mnuPaintStyle.ImageIndex = -1;
        }

		#endregion

        #region Window placing

        private Boolean IsTabbedMdi 
        { 
            get { return mnuTabbed.Down; } 
        }

        void InitTabbedMDI()
        {
            mdiManager1.MdiParent = IsTabbedMdi ? this : null;
            mnuCascade.Visibility = mnuHorizontal.Visibility = mnuVertical.Visibility = IsTabbedMdi ? BarItemVisibility.Never : BarItemVisibility.Always;
        }


        private void mnuTabbed_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitTabbedMDI();
        }


        private void mnuCascade_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }


        private void mnuHorizontal_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }


        private void mnuVertical_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        #endregion

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Version ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            this.Text = ProgramMain.Framework.GlobalData.GetAssemblyTitle() + " v " + ver.ToString(2) + " - [" + ProgramMain.Framework.BaseSecurity.UserName + "]";
            
            if (Properties.Settings.Default.AutoShowMap
                && (BaseSecurity.CheckSecurityAction(BaseSecurity.SecurityActionType.View) != BaseSecurity.SuperBool.False) || BaseSecurity.CheckSecurityManagerAccess() == BaseSecurity.SuperBool.True)
            {
                FrmOpticMap.ShowForm();
            }

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.NavigatorState =
                nbcMain.OptionsNavPane.NavPaneState == DevExpress.XtraNavBar.NavPaneState.Expanded;
            Properties.Settings.Default.Save();
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            Screen ProgramScreen = Screen.FromControl(Program.MainForm);
            ScreenDeviceName = ProgramScreen.DeviceName;
        }

        private void mnuExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void mnuAbout_ItemClick(object sender, ItemClickEventArgs e)
        {
            nbiAbout_LinkClicked(sender, new DevExpress.XtraNavBar.NavBarLinkEventArgs(this.nbgHelp.ItemLinks[0]));
        }

        private void nbiAbout_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FrmAbout frm = new FrmAbout();
            frm.ShowDialog(Program.MainForm);
        }

        private void mnuUpdate_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (ProgramMain.Framework.GlobalData.UpdateApplication() == false)
            {
                XtraMessageBox.Show("Установлена последняя версия приложения", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void timerUpdateCheck_Tick(object sender, EventArgs e)
        {
            ProgramMain.Framework.GlobalData.UpdateApplication();
            System.Random random = new Random(DateTime.Now.Millisecond);
            int start = 3600*2*1000;
            int finish = 3600*3*1000;
            timerUpdateCheck.Interval = random.Next(start, finish);
        }

        private void mnuParameters_ItemClick(object sender, ItemClickEventArgs e)
        {
            ProgramParametersCtl frmParameters = new ProgramParametersCtl();
            frmParameters.ShowModal();
        }

        private void mnuLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            Properties.Settings.Default.UserName = "";
            Properties.Settings.Default.UserPassword = "";
            Properties.Settings.Default.Save();
            Application.Restart();
        }
    
        private void nbiMap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmOpticMap.ShowForm();
            }
        }

        private void nbiVertexTypes_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmVertexTypes.ShowForm();
            }
        }

        private void nbiOwnerCompanies_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmOwnerCompanies.ShowForm();
            }
        }

        private void nbiOwnerColors_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmColor.ShowForm();
            }
        }

        private void nbiVertexes_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmVertexes.ShowForm();
            }
        }

        private void nbiCableTunnels_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmCableTunnels.ShowForm();
            }
        }

        private void nbiCurveElementTypes_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmCurveElementType.ShowForm();
            }
        }

        private void nbiCableBoxes_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmCableBoxes.ShowForm();
            }
        }

        private void nbiEquipment_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmEquipment.ShowForm();
            }
        }

        private void nbiCables_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmCables.ShowForm();
            }
        }

        private void nbiUpdate_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (ProgramMain.Framework.GlobalData.UpdateApplication() == false)
            {
                XtraMessageBox.Show("Установлена последняя версия приложения", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void nbiCableTunnelType_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (Framework.BaseSecurity.CheckSecurityViewAction())
            {
                FrmCableTunnelType.ShowForm();
            }
        }
    }
}